package com.ruoyi.web.controller.riskAssessment;

import com.github.pagehelper.PageHelper;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.pengjie.SnowflakeIdUtils;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.riskAssessment.domain.RkCard;
import com.ruoyi.riskAssessment.dto.RkCardDto;
import com.ruoyi.riskAssessment.service.IRkCardService;
import com.ruoyi.riskAssessment.vo.RiskIdentifyEvaluationVo;
import com.ruoyi.riskAssessment.vo.RkCardVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
import java.util.List;
import java.util.Random;

/**
 * 风险-三卡管理Controller
 *
 * @author lth
 * @date 2023-12-15
 */
@RestController
@RequestMapping("/riskAssessment/card")
public class RkCardController extends BaseController
{
    @Autowired
    private IRkCardService rkCardService;

    /**
     * 查询风险-三卡管理列表
     */
    @PreAuthorize("@ss.hasPermi('riskAssessment:card:list')")
    @GetMapping("/list")
    public TableDataInfo list(RkCard rkCard)
    {
        startPage();
        List<RkCard> list = rkCardService.selectRkCardList(rkCard);
        return getDataTable(list);
    }

    /**
     * 导出风险-三卡管理列表
     */
    @PreAuthorize("@ss.hasPermi('riskAssessment:card:export')")
    @Log(title = "风险-三卡管理", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, RkCard rkCard)
    {
        List<RkCard> list = rkCardService.selectRkCardList(rkCard);
        ExcelUtil<RkCard> util = new ExcelUtil<RkCard>(RkCard.class);
        util.exportExcel(response, list, "风险-三卡管理数据");
    }

    /**
     * 获取风险-三卡管理详细信息
     */
    @PreAuthorize("@ss.hasPermi('riskAssessment:card:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return success(rkCardService.selectRkCardById(id));
    }

    /**
     * 新增风险-三卡管理
     */
    @PreAuthorize("@ss.hasPermi('riskAssessment:card:add')")
    @Log(title = "风险-三卡管理", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody RkCard rkCard)
    {
        long id = new SnowflakeIdUtils(5, 8).nextId();
        rkCard.setId(id);
        String r = generateRandomString(3);
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyMMdd");
        String currentDate = LocalDate.now().format(formatter);
        String code = "PG" + currentDate + r;
        rkCard.setCode(code);

        return toAjax(rkCardService.insertRkCard(rkCard));
    }

    private static String generateRandomString(int length) {
        String chars = "0123456789abcdefghijklmnopqrstuvwxyz";
        StringBuilder sb = new StringBuilder(length);
        Random random = new Random();
        for (int i = 0; i < length; i++) {
            char c = chars.charAt(random.nextInt(chars.length()));
            sb.append(c);
        }
        return sb.toString();
    }

    /**
     * 修改风险-三卡管理
     */
    @PreAuthorize("@ss.hasPermi('riskAssessment:card:edit')")
    @Log(title = "风险-三卡管理", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody RkCard rkCard)
    {
        return toAjax(rkCardService.updateRkCard(rkCard));
    }

    /**
     * 删除风险-三卡管理
     */
    @PreAuthorize("@ss.hasPermi('riskAssessment:card:remove')")
    @Log(title = "风险-三卡管理", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(rkCardService.deleteRkCardByIds(ids));
    }


    /**
     *  三卡管理--制作卡牌--选择风险列表
     */
    @PreAuthorize("@ss.hasPermi('riskAssessment:card:query')")
    @GetMapping("/listByVo")
    public TableDataInfo listByVo(@RequestBody RkCardVo vo)
    {
        PageHelper.startPage(vo.getPageNum(),vo.getPageSize());
        List<RkCardDto> dtoList = rkCardService.listByVo(vo);
        return getDataTable(dtoList);
    }


}
